<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 头部公司信息 -->
        <TreeTools :tree-data="company" :is-root="true" />
        <hr>
        <!-- 树形组件 -->
        <el-tree
          :default-expand-all="true"
          :data="departs"
          :props="{ label: 'name' }"
        >
          <!-- 这里利用插槽的形式, 自定义树形节点渲染
          因为树形遍历是在饿了么内部完成的
          所以需要作用域插槽 slot-scope 获取每一个被遍历到的部门
          传入子组件当中, 进行显示 -->
          <TreeTools slot-scope="scope" :tree-data="scope.data" @addDepts="addDepts" @delDepts="getDepartments" />
        </el-tree>
      </el-card>

      <!-- <el-dialog visible="true">
        hhh
      </el-dialog> -->
      <add-depts :show-dialog="showDialog" />
    </div>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils/index'
import { getDepartments } from '@/api/departments'
import TreeTools from './components/tree-tools'
import AddDepts from './components/add-depts'
export default {
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      showDialog: false, // 显示弹窗
      company: {}, // 头部数据
      departs: [], // 树形结构数据
      defaultProps: {
        label: 'name' // 表示从这个属性显示内容
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      this.company = { name: res.companyName, manager: '负责人' }
      this.departs = tranListToTreeData(res.depts, '')
      console.log(res)
    },
    addDepts(data) {
      this.showDialog = true // 显示弹窗
      this.data = data
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
